package com.yuan;

import java.util.Arrays;

/**
 * @ClassName: toright
 * @Description: 将字符串向右循环移动 k 位
 * @Author MrYuan
 * @Date: 2021/3/2 15:34
 * @Version 1.0
 */
public class toright {
    public static void main(String[] args) {
        String str = "abcd123";
        char[] chars = str.toCharArray();
        int length = str.length();
        movetoright(chars, length, 3);
        System.out.println(Arrays.toString(chars));
    }

    public static void movetoright(char[] str, int size, int k) {
        reverse(str, 0, size - k - 1);
        reverse(str, size - k, size - 1);
        reverse(str, 0, size - 1);
    }

    public static void reverse(char[] str, int left, int right) {
        while (left < right) {
            swap(str, left, right);
            left++;
            right--;
        }
    }

    public static void swap(char[] str, int c1, int c2) {
        char temp;
        temp = str[c1];
        str[c1] = str[c2];
        str[c2] = temp;
    }
}